package com.tannm.doan.taskmanager.reminder;

import java.util.*;

import com.tannm.doan.taskmanager.Utils;
import com.tannm.doan.taskmanager.agenda.*;
import com.tannm.doan.taskmanager.database.*;
import com.tannm.doan.taskmanager.dataview.*;

import android.content.Context;

public class AlarmDataView {
	// Comparator type for appointments
	public class ApptsItemsComparator implements Comparator<AlarmDataViewItem> {
		public int compare(AlarmDataViewItem item1, AlarmDataViewItem item2) {
			if (item1.isOverdue() || item2.isOverdue()) {
				// overdue items for sorting
				if (item1.getOverdueDays() < item2.getOverdueDays())
					return 1;
				if (item1.getOverdueDays() > item2.getOverdueDays())
					return -1;

				if (item1.getOverdueDays() == item2.getOverdueDays()) {
					String s1 = item1.sSubject;
					String s2 = item2.sSubject;
					return s1.compareTo(s2);
				}

				return 0;
			} else {
				// today items for sorting
				if (item1.timeAsSeconds() == item2.timeAsSeconds()) {
					String s1 = item1.sSubject;
					String s2 = item2.sSubject;
					return s1.compareTo(s2);
				} else {

					if (item1.timeAsSeconds() > item2.timeAsSeconds())
						return 1;
					if (item1.timeAsSeconds() < item2.timeAsSeconds())
						return -1;

					if (item1.timeAsSeconds() == item2.timeAsSeconds()) {
						String s1 = item1.sSubject;
						String s2 = item2.sSubject;
						return s1.compareTo(s2);
					}
				}

			}
			return 0;
		}
	};

	// Comparator type for tasks
	public class TasksItemsComparator implements Comparator<AlarmDataViewItem> {
		public int compare(AlarmDataViewItem item1, AlarmDataViewItem item2) {
			if (item1.lPriority == item2.lPriority) {
				String s1 = item1.sSubject;
				String s2 = item2.sSubject;
				return s1.compareTo(s2);
			} else {

				if (item1.lPriority > item2.lPriority)
					return 1;
				if (item1.lPriority < item2.lPriority)
					return -1;

				if (item1.lPriority == item2.lPriority) {
					String s1 = item1.sSubject;
					String s2 = item2.sSubject;
					return s1.compareTo(s2);
				}
			}
			return 0;
		}
	};

	// fields
	protected ArrayList<AlarmDataViewItem> alarmItemsAppts = new ArrayList<AlarmDataViewItem>();
	protected ArrayList<AlarmDataViewItem> alarmItemsTasks = new ArrayList<AlarmDataViewItem>();
	private ApptsItemsComparator fnApptsItemsCmp = null;
	private TasksItemsComparator fnTasksItemsCmp = null;

	// fields
	protected Context ctx = null;
	protected Database db = null;
	protected Utils utils = null;
	protected AlarmService.AlarmPrefs prefs = null;

	// fields
	private DataViewAppointment dataViewAppt = null;
	private DataViewTask dataViewTask = null;

	// fields
	private final int agendaViewType = AgendaView.viewMode.TODAY_ALARM;
	private Calendar calStartDate = Calendar.getInstance();
	private AlarmsManager alarmsManager = null;

	// methods
	public AlarmDataView(Context ctx, Database db,
			AlarmService.AlarmPrefs prefs, Utils utils) {
		this.ctx = ctx;
		this.db = db;
		this.prefs = prefs;
		this.utils = utils;

		// create alarsm manager instance
		alarmsManager = new AlarmsManager(this.db, this.prefs);

		this.fnApptsItemsCmp = new ApptsItemsComparator();
		this.fnTasksItemsCmp = new TasksItemsComparator();

		dataViewAppt = new DataViewAppointment(db, prefs);
		dataViewTask = new DataViewTask(db, prefs);
	}

	private boolean reloadData() {
		if (db.databaseReady()) {
			dataViewAppt.reloadTable();
			dataViewTask.reloadTable();

			dataViewAppt.filterData(calStartDate, agendaViewType);
			dataViewTask.filterData(calStartDate, agendaViewType);

			return true;
		}

		return false;
	}

	public boolean collectAlarmItems() {
		calStartDate.setTimeInMillis(System.currentTimeMillis());

		final int iHour = calStartDate.get(Calendar.HOUR_OF_DAY);
		final int iMinute = calStartDate.get(Calendar.MINUTE);
		final int iCurrTimeKey = (iHour * 100) + iMinute;

		alarmItemsAppts.clear();
		alarmItemsTasks.clear();

		if (reloadData()) {

			// collect appointments
			for (int i = 0; i < dataViewAppt.getRowsCountTotal(); i++) {
				DataViewItem row = dataViewAppt.getRow(i, agendaViewType);
				if ((row != null) && (row.bAlarm)) {
					if (row.timeOverdue(iCurrTimeKey)) {
						if (alarmsManager.canShowAlarmToday(
								AlarmDataViewItem.iOrderAppts, row.lID, row
										.isRepeat())) {
							AlarmDataViewItem item = new AlarmDataViewItem(
									row.lID, row.sSubject,
									AlarmDataViewItem.iOrderAppts, row.bAlarm);
							item.setRepeatDays(row.getVisibleDays());
							item.set(row.getStartHour(), row.getStartMinute(),
									row.getDuration());
							alarmItemsAppts.add(item);
						}
					}
				}
			}

			// collect tasks
			for (int i = 0; i < dataViewTask.getRowsCountTotal(); i++) {
				DataViewItem row = dataViewTask.getRow(i, agendaViewType);
				if ((row != null) && (row.bAlarm)) {
					if (alarmsManager.canShowAlarmToday(
							AlarmDataViewItem.iOrderTasks, row.lID, row
									.isRepeat())) {
						AlarmDataViewItem item = new AlarmDataViewItem(row.lID,
								row.sSubject, AlarmDataViewItem.iOrderTasks,
								row.bAlarm);
						item.set(row.getPriority());
						alarmItemsTasks.add(item);
					}
				}
			}

			Collections.sort(alarmItemsAppts, fnApptsItemsCmp);
			Collections.sort(alarmItemsTasks, fnTasksItemsCmp);

			return true;
		}
		return false;
	}

	public ArrayList<AlarmDataViewItem> getApptsData() {
		return alarmItemsAppts;
	}

	public ArrayList<AlarmDataViewItem> getTasksData() {
		return alarmItemsTasks;
	}

}
